package com.cclinux.projects.actcampus.controller.admin;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cclinux.framework.annotation.DemoShow;
import com.cclinux.framework.core.domain.ApiResult;
import com.cclinux.framework.core.domain.PageParams;
import com.cclinux.framework.core.domain.PageResult;
import com.cclinux.framework.core.mapper.UpdateWhere;
import com.cclinux.framework.core.mapper.Where;
import com.cclinux.framework.helper.FormHelper;
import com.cclinux.framework.validate.DataCheck;
import com.cclinux.projects.actcampus.mapper.SettingMapper;
import com.cclinux.projects.actcampus.model.ActivityJoinClassModel;
import com.cclinux.projects.actcampus.model.NewsModel;
import com.cclinux.projects.actcampus.model.SettingModel;
import com.cclinux.projects.actcampus.service.admin.AdminNewsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController("ActCampusAdminConfigController")
public class AdminConfigController extends BaseMyAdminController {

    @Resource(name = "ActCampusSettingMapper")
    private SettingMapper settingMapper;


    @RequestMapping(value = "/admin/config/list")
    public ApiResult getAdminNewsList(@RequestBody Map<String, Object> input) {

        // 数据校验
        String[] RULES = PageParams.PAGE_CHECK_RULE;
        DataCheck.check(input, RULES);

        // 业务处理
        Where<SettingModel> where = new Where<>();
        where.eq("STATUS", 0);
        Page page = new Page(0, 10);
        PageResult settingModelList = settingMapper.getPageList(page, where, "*");

        return ApiResult.success(settingModelList);
    }


    @RequestMapping(value = "/admin/config/detail")
    public ApiResult getNewsDetail(@RequestBody Map<String, Object> input) {

        // 数据校验

        // 业务
        long id = MapUtil.getLong(input, "id");
        SettingModel settingModel = settingMapper.getOne(id);

        return ApiResult.success(settingModel);
    }

    @DemoShow
    @RequestMapping(value = "/admin/config/edit")
    public ApiResult editNews(@RequestBody Map<String, Object> input, @RequestAttribute long adminId) {

        // 数据校验
//        String[] RULES = {
//                "id:must|long",
//                "title:must|string|min=4|max=50|name=标题",
//                "cateId:must|long|name=分类",
//                "cateName:must|string|name=分类名",
//                "order:must|long|min=0|max=9999|name=排序号",
//                "forms:must|string|name=表单",
//                "obj:must|string|name=表单",
//        };
//        DataCheck.check(input, RULES);


        // 业务
        SettingModel news = this.inputToNews(input);

        if (!isJson(news.getNewsValue())) {

            return ApiResult.error("格式不正确，请检验");
        }

        UpdateWhere<SettingModel> uw = new UpdateWhere<>();
        uw.eq("NEWS_ID", news.getNewsId());
        uw.set("NEWS_VALUE", news.getNewsValue());
        settingMapper.edit(uw);

        return ApiResult.success();
    }

    public static boolean isJson(String jsonStr) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(jsonStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    private SettingModel inputToNews(Map<String, Object> input) {
        SettingModel model = new SettingModel();
        model.setNewsId(MapUtil.getLong(input, "id"));
        model.setNewsName(MapUtil.getStr(input, "newsName"));
        model.setNewsValue(MapUtil.getStr(input, "newsValue"));

        return model;
    }
}
